package com.fjwt.gz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fjwt.gz.core.exception.BizException;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.FansPayDTO;
import com.fjwt.gz.db.entity.*;
import com.fjwt.gz.service.mapper.ActivityAwardMapper;
import com.fjwt.gz.service.mapper.FansPayMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 卡券三方出资公众号openid表（三方同主体用来发券） 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class FansPayService extends ServiceImpl<FansPayMapper, FansPayEntity> {

    @Autowired
    private FansPayMapper fansPayMapper;

    @Autowired
    private ScoreExchangeService scoreExchangeService;

    @Autowired
    private PayAuthService payAuthService;

    @Autowired
    private SysConfigService sysConfigService;

    @Autowired
    private ActivityAwardMapper activityAwardMapper;

    @Autowired
    private ActivityService activityService;


    public void addFansPay(FansPayDTO dto) {
        if (dto.getFansId() == null || StringUtils.isEmpty(dto.getIfCode())
                || StringUtils.isEmpty(dto.getOpenId())){
            throw new BizException("网络故障，请稍后重试！");
        }
        FansPayEntity fansPayEntity = BeanUtil.copyProperties(dto, FansPayEntity.class);
        LambdaQueryWrapper<FansPayEntity> gw = FansPayEntity.gw();
        gw.eq(FansPayEntity::getFansId, fansPayEntity.getFansId());
        gw.eq(FansPayEntity::getOpenId, fansPayEntity.getOpenId());
        gw.eq(FansPayEntity::getIfCode, fansPayEntity.getIfCode());
        FansPayEntity fansPayDb = fansPayMapper.selectOne(gw);
        if (ObjectUtil.isNull(fansPayDb)) {
            fansPayEntity.setCreatedAt(new Date());
            fansPayMapper.insert(fansPayEntity);
        }
    }

    /**
     * 校验用户券是否发券
     * @param fansId
     * @param scoreExchangeId
     * @return
     */
    public JSONObject checkUserPayAuth(Long fansId, String scoreExchangeId) {
        if(null == fansId || StringUtils.isEmpty(scoreExchangeId)){
            throw new BizException("网络故障，请稍后重试！");
        }
        JSONObject json = new JSONObject();
        ScoreExchangeEntity exchangeEntity = scoreExchangeService.getBaseMapper().selectById(scoreExchangeId);
        if(null == exchangeEntity){
            throw new BizException("此奖品不存在");
        }
        Long payAuthId = exchangeEntity.getPayAuthId();
        PayAuthEntity payAuthEntity = payAuthService.getById(payAuthId);
        if(null == payAuthEntity){
            json.put("flag",true);
            return json;
        }
        SysConfig sysConfig = sysConfigService.getById(payAuthEntity.getIfCode());
        JSONObject object = JSONObject.parseObject(sysConfig.getConfigVal());
        if(payAuthEntity.getIsCommSubject()==0){
            LambdaQueryWrapper<FansPayEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FansPayEntity::getIfCode,payAuthEntity.getIfCode());
            queryWrapper.eq(FansPayEntity::getFansId,fansId);
            FansPayEntity one = fansPayMapper.selectOne(queryWrapper);
            if(null == one){
                json.put("flag",false);
                json.put("ifCode",payAuthEntity.getIfCode());
                json.put("url",object.getString("url"));
            }else{
                json.put("flag",true);
                json.put("openId",one.getOpenId());

            }
        }else{
            json.put("flag",true);
        }
        return json;
    }

    /**
     * 校验用户券是否授权
     * @param fansId
     * @param
     * @return
     */
    public JSONObject getcouponPayAuth(Long fansId, String awardId) {
        JSONObject json = new JSONObject();

        LambdaQueryWrapper<ActivityAwardEntity> activityAwardEntity = new LambdaQueryWrapper<>();
        activityAwardEntity.eq(ActivityAwardEntity::getAwardId,awardId);
        activityAwardEntity.eq(ActivityAwardEntity::getAwardType,0);
        ActivityAwardEntity activityAward = activityAwardMapper.selectOne(activityAwardEntity);

        ActivityEntity activity = activityService.getBaseMapper().selectById(activityAward.getActivityId());
        if(null == activity){
            throw new BizException("此卡券不存在，请联系管理员");
        }
        Long payAuthId = activity.getPayAuthId();
        PayAuthEntity payAuthEntity = payAuthService.getById(payAuthId);
        if(null == payAuthEntity){
            json.put("flag",true);
            return json;
        }
        SysConfig sysConfig = sysConfigService.getById(payAuthEntity.getIfCode());
        JSONObject object = JSONObject.parseObject(sysConfig.getConfigVal());
        if(payAuthEntity.getIsCommSubject()==0){
            LambdaQueryWrapper<FansPayEntity> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(FansPayEntity::getIfCode,payAuthEntity.getIfCode());
            queryWrapper.eq(FansPayEntity::getFansId,fansId);
            FansPayEntity one = fansPayMapper.selectOne(queryWrapper);
            if(null == one){
                json.put("flag",false);
                json.put("ifCode",payAuthEntity.getIfCode());
                json.put("url",object.getString("url"));
            }else{
                json.put("flag",true);
            }
        }else{
            json.put("flag",true);
        }
        return json;
    }
}
